﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace FGrubert.CrmQueueMonitor.WinForms
{
    /// <summary>
    /// Represents a single queue
    /// </summary>
    public class Queue : QueueBase
    {
        /// <summary>
        /// Creates a queue with empty data
        /// </summary>
        public Queue()
        {
        }

        /// <summary>
        /// Creates a queue with the data of a queue base
        /// </summary>
        /// <param name="queueBase">The queue base data</param>
        public Queue(QueueBase queueBase)
        {
            this.QueueId = queueBase.QueueId;
            this.HoursToRed = queueBase.HoursToRed;
            this.HoursToYellow = queueBase.HoursToYellow;
            this.Name = queueBase.Name;
        }

        private int _numItems;
        /// <summary>
        /// the number of items in the queue
        /// </summary>
        public int NumItems
        {
            get { return _numItems; }
            set
            {
                if (_numItems != value)
                {
                    _numItems = value;
                    NotifyPropertyChanged();
                }
            }
        }

        private int _numNotWorkedOn;
        /// <summary>
        /// The number of items not worked on in the queue
        /// </summary>
        public int NumNotWorkedOn
        {
            get { return _numNotWorkedOn; }
            set
            {
                if (_numNotWorkedOn != value)
                {
                    _numNotWorkedOn = value;
                    NotifyPropertyChanged();
                }
            }
        }

        private DateTime? _latest;
        /// <summary>
        /// The date and time of the latest item in the queue
        /// </summary>
        public DateTime? Latest
        {
            get { return _latest; }
            set
            {
                if (_latest != value)
                {
                    _latest = value;
                    NotifyPropertyChanged();
                }
            }
        }

        private DateTime? _oldest;
        /// <summary>
        /// The date and time of the oldest item in the queue 
        /// </summary>
        public DateTime? Oldest
        {
            get { return _oldest; }
            set
            {
                if (_oldest != value)
                {
                    _oldest = value;
                    NotifyPropertyChanged();
                    NotifyPropertyChanged("LongestTime");
                    NotifyPropertyChanged("Status");
                }
            }
        }

        /// <summary>
        /// Returns the time that the oldest queue item is in the queue
        /// </summary>
        public TimeSpan? LongestTime
        {
            get
            {
                return DateTime.Now - Oldest;
            }
        }

        /// <summary>
        /// Returns the image for the status of the current queue
        /// </summary>
        public QueueStatus Status
        {
            get
            {
                if (!LongestTime.HasValue || (HoursToRed <= 0 && HoursToYellow <= 0))
                {
                    return QueueStatus.Neutral;
                }
                else if (HoursToRed > 0 && LongestTime.Value.TotalHours > HoursToRed)
                {
                    return QueueStatus.Red;
                }
                else if (HoursToYellow > 0 && LongestTime.Value.TotalHours > HoursToYellow)
                {
                    return QueueStatus.Yellow;
                }
                else
                {
                    return QueueStatus.Green;
                }
            }
        }

        private DateTime _lastUpdated;
        /// <summary>
        /// Gets or sets the date and time the queue information was updated last
        /// </summary>
        public DateTime LastUpdated
        {
            get { return _lastUpdated; }
            set
            {
                _lastUpdated = value;
                NotifyPropertyChanged();
                NotifyPropertyChanged("LongestTime");
                NotifyPropertyChanged("Status");
            }
        }
    }
}
